package com.server.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.common.base.PathManager;
import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.IMsgManagementService;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("")
public class IndexController {

	@Autowired
	private IMsgManagementService msgManagementService;
	
	@RequestMapping("index")
	public ModelAndView index(HttpServletRequest request) {
		/*
		 * 记录Http地址
		 */
		String requestUrl = msgManagementService.getRequestUrl();
		if (StringUtils.isEmpty(requestUrl)) {
			msgManagementService.setRequestUrl(request.getRequestURL().toString());
		}
		ModelAndView mv = new ModelAndView("index");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		
		StringBuffer serverBuffer = new StringBuffer();
		String serverName = request.getServerName();
		int serverPort = request.getServerPort();
		serverBuffer.append("http://").append(serverName)
		.append(":").append(serverPort).append(ProjectInfor.ROOT_PATH);
		mv.addObject("homePath",serverBuffer.toString());
		
		return mv;
	}

	@RequestMapping("")
	public ModelAndView home(HttpServletRequest request) {
		/*
		 * 记录Http地址
		 */
		String requestUrl = msgManagementService.getRequestUrl();
		if (StringUtils.isEmpty(requestUrl)) {
			msgManagementService.setRequestUrl(request.getRequestURL().toString());
		}
		String tabId = request.getParameter("tabId");
		
		ModelAndView mv = null;
		mv = new ModelAndView("Index/home");
		if (!StringUtils.isEmpty(SessionUtil.getStringAttri(request, SessionData.nameKey))) {
			mv.addObject("userName", SessionUtil.getStringAttri(request, SessionData.nameKey));
		}
		if (!StringUtils.isEmpty(SessionUtil.getStringAttri(request, SessionData.uuidKey))) {
			mv.addObject("uuid", SessionUtil.getStringAttri(request, SessionData.uuidKey));
		}
		mv.addObject("url",  SessionUtil.getStringAttri(request, SessionData.photoUrlKey));
		mv.addObject("tabId",tabId);
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		
		StringBuffer serverBuffer = new StringBuffer();
		String serverName = request.getServerName();
		int serverPort = request.getServerPort();
		serverBuffer.append("http://").append(serverName)
		.append(":").append(serverPort).append("/")
		.append(PathManager.FILE_MAPPING);
		mv.addObject("fileServer",serverBuffer.toString());
		mv.addObject("platformName",ProjectInfor.PLATFORM_NAME);
		
		//mv.addObject("fileServer",PathManager.FILE_SERVER_ATTR);
		return mv;
	}

}